//
//  UIView+TYExtendsion.m
//  TYToolKit
//
//  Created by dev_tianyu on 16/8/6.
//  Copyright © 2016年 dev_tianyu. All rights reserved.
//

#import "UIView+TYExtendsion.h"

@implementation UIView (TYExtendsion)

/**
 *  对view进行截图
 *
 *  @return
 */
- (UIImage*)convertViewToImage {
    
    CGSize size = self.bounds.size;
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

/**
 *  全屏截图
 *
 *  @return
 */
+ (UIImage *) shotScreen {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIGraphicsBeginImageContext(window.bounds.size);
    [window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

/**
 *  移除view上的所有子视图
 */
- (void) removeAllSubViews {
    for (UIView *subView in self.subviews) {
        [subView removeFromSuperview];
    }
}
@end
